// DarkSpaceDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DarkSpace.h"
#include "DarkSpaceDlg.h"
#include ".\darkspacedlg.h"

#ifdef _DEBUG
//#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDarkSpaceDlg dialog

CDarkSpaceDlg::CDarkSpaceDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDarkSpaceDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDarkSpaceDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDarkSpaceDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDarkSpaceDlg)
	DDX_Control(pDX, IDC_BITMAP, m_Bitmap);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDarkSpaceDlg, CDialog)
	//{{AFX_MSG_MAP(CDarkSpaceDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDPLAY, OnPlay)
	ON_BN_CLICKED(IDCREDITS, OnCredits)
	ON_BN_CLICKED(IDSETUP, OnSetup)
	ON_BN_CLICKED(IDQUIT, OnQuit)
	ON_BN_CLICKED(IDMISSION, OnMission)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDTUTORIAL, OnTutorial)
	ON_BN_CLICKED(IDEDITOR, OnEditor)
	//}}AFX_MSG_MAP
	ON_STN_CLICKED(IDC_BITMAP, OnStnClickedBitmap)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDarkSpaceDlg message handlers

//----------------------------------------------------------------------------

static CString GetKeyValue( const TCHAR * pKey, const TCHAR * pDefault )
{
	TCHAR sValue[ 1024 ];
	sValue[ 0 ] = 0;

	GetPrivateProfileString( _T("DarkSpace"), pKey, pDefault != NULL ? pDefault : _T(""), sValue, 1024, _T(".\\config.ini") );
	return CString( sValue );
}

static HANDLE StartProcess( const TCHAR * pCommandLine )
{
    STARTUPINFO			si;
    PROCESS_INFORMATION pi;

	memset( &si, 0, sizeof(si) );
    si.cb = sizeof(si);

	char sCommandLine[ MAX_PATH ];
	strcpy( sCommandLine, pCommandLine );

	if ( !CreateProcess( NULL,					// No module name (use command line). 
        sCommandLine,							// Command line. 
        NULL,									// Process handle not inheritable. 
        NULL,									// Thread handle not inheritable. 
        FALSE,									// Set handle inheritance to FALSE. 
        CREATE_NEW_PROCESS_GROUP,				// Creation flags. 
        NULL,									// Use parent's environment block. 
        NULL,									// Use parent's starting directory. 
        &si,									// Pointer to STARTUPINFO structure.
        &pi ))									// Pointer to PROCESS_INFORMATION structure.
	{
		// failed
		return NULL;
	}

	return( pi.hProcess );
}

//----------------------------------------------------------------------------


BOOL CDarkSpaceDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.
	ModifyStyleEx( 0, WS_EX_TOPMOST );

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// load bitmap
	HANDLE hBmp = LoadImage( 0, GetKeyValue( "Splash", "dsu.bmp") , IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
	if ( hBmp != NULL )
		m_Bitmap.SetBitmap( (HBITMAP)hBmp );

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	m_PlayButton.Attach( IDPLAY, this, RGB( 64, 0, 0 ), RGB (255,255,255), RGB( 64, 0, 0 ) );
	m_TutorialButton.Attach( IDTUTORIAL, this, RGB( 64, 0, 0 ), RGB (255,255,255), RGB( 64, 0, 0 ) );
	m_MissionButton.Attach( IDMISSION, this, RGB( 64, 0, 0 ), RGB (255,255,255), RGB( 64, 0, 0 ) );
	m_EditorButton.Attach( IDEDITOR, this, RGB( 64, 0, 0 ), RGB (255,255,255), RGB( 64, 0, 0 ) );
	m_CreditsButton.Attach( IDCREDITS, this, RGB( 64, 0, 0 ), RGB (255,255,255), RGB( 64, 0, 0 ) );
	m_SetupButton.Attach( IDSETUP, this, RGB( 64, 0, 0 ), RGB (255,255,255), RGB( 64, 0, 0 ) );
	m_QuitButton.Attach( IDQUIT, this, RGB( 64, 0, 0 ), RGB (255,255,255), RGB( 64, 0, 0 ) );

	ShowWindow( SW_SHOWMAXIMIZED );

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDarkSpaceDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDarkSpaceDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDarkSpaceDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//----------------------------------------------------------------------------

void CDarkSpaceDlg::OnPlay() 
{
	// goto the lobby
	if ( StartProcess( GetKeyValue( "OnLobby", "run.bat tsgw1.dyndns.org 9001 420" ) ) == NULL )
		MessageBox( _T("Failed to start lobby, please reinstall!") );
	else
		EndDialog( IDOK );
}

void CDarkSpaceDlg::OnTutorial() 
{
	// goto the lobby
	if ( StartProcess( GetKeyValue( "OnTutorial", "run.bat tsgw1.dyndns.org 9002 420") ) == NULL )
		MessageBox( _T("Failed to start tutorial, please reinstall!") );
	else
		EndDialog( IDOK );
}

void CDarkSpaceDlg::OnCredits() 
{
	if ( StartProcess( GetKeyValue( "OnCredits", "run.bat tsgw1.dyndns.org 9002") ) == NULL )
		MessageBox( _T("Failed to start tutorial, please reinstall!") );
	else
		EndDialog( IDOK );	
}

void CDarkSpaceDlg::OnSetup() 
{
	if ( StartProcess( GetKeyValue( "OnSetup", "update.bat dsurelease P@55W0rD") ) == NULL )
		MessageBox( _T("Failed to run setup, please reinstall!") );
}

void CDarkSpaceDlg::OnMission() 
{
	if ( StartProcess( GetKeyValue( "OnMission", "run.bat tsgw1.dyndns.org 9004 420") ) == NULL )
		MessageBox( _T("Failed to start mission viewer, please reinstall!") );
	else
		EndDialog( IDOK );
}

void CDarkSpaceDlg::OnEditor() 
{
	if ( StartProcess( GetKeyValue( "OnEditor", "run.bat tsgw1.dyndns.org 9005 420") ) == NULL )
		MessageBox( _T("Failed to start editor, please reinstall!") );
	else
		EndDialog( IDOK );
}

void CDarkSpaceDlg::OnQuit() 
{
	EndDialog( 0 );	
}

void CDarkSpaceDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	if ( ::IsWindow( m_Bitmap.m_hWnd ) )
	{
		// center the bitmap
		CRect br;
		m_Bitmap.GetClientRect( &br );

		int left = (cx / 2) - (br.Width() / 2);
		int top = (cy / 2) - (br.Height() / 2);
		m_Bitmap.MoveWindow( (cx / 2) - (br.Width() / 2), 
			(cy / 2) - (br.Height() / 2), 
			br.Width(), br.Height() );

		// move the buttons
		int x = left;
		if ( x < 0 )
			x = 0;

		int y = top - 33;
		if ( y < 0 )
			y = 0;

		m_PlayButton.MoveWindow( x, y, 128, 31 );
		x += 130;
		m_TutorialButton.MoveWindow( x, y, 128, 31 );
		x += 130;
		m_MissionButton.MoveWindow( x, y, 128, 31 );
		x += 130;
		m_EditorButton.MoveWindow( x, y, 128, 31 );
		x += 130;
		m_SetupButton.MoveWindow( x, y, 128, 31 );
		x += 130;
		m_QuitButton.MoveWindow( x, y, 128, 31 );
	}
}



void CDarkSpaceDlg::OnStnClickedBitmap()
{
	if ( StartProcess( GetKeyValue( "OnStnClickedBitmap", "dsu.url" ) ) == NULL )
		MessageBox( _T("Failed!") );
	else
		EndDialog( IDOK );
}
